<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateOrderExtOrderMappingTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    private $name;

    public function up()
    {

        $cFunc = function (Blueprint $table) {
            $table->unsignedBigInteger('id')->primary();
            $table->unsignedInteger('brandId')->default(0)->comment('品牌id');
            $table->unsignedInteger('shopId')->default(0)->comment('店铺 ID');
            $table->unsignedTinyInteger('type')->default(0)->comment('类型 1.券包订单 2.权益卡');
            $table->unsignedBigInteger('orderId')->comment("我方订单 ID");
            $table->unsignedBigInteger('extOrderId')->comment("其他订单 ID");
            $table->unsignedInteger('payPrice')->default(0)->comment('其他订单的实际支付金额');

            // 统一表字段
            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('更新时间');
            $table->unsignedInteger('createdByUid')->default(0)->comment('创建人');
            $table->unsignedInteger('updatedByUid')->default(0)->comment('更新人');

            $table->index('brandId', 'idx_brandId');
            $table->index('shopId', 'idx_shopId');
            $table->index('createdAt', 'idx_createdAt');
            $table->index('orderId', 'idx_orderId');
            $table->index('extOrderId', 'idx_extOrderId');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '订单关联表' . $this->name;
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->create('order_external' . $this->name, $cFunc);
        }

        $this->name = '';
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->dropIfExists('order_external' . $name);
        }
    }
}
